<?php

namespace zhtfrontend\controllers;

use common\models\datas\MessageLog;
use doumiaoduo\buildform\BuildForm;
use yii\data\Pagination;
use yii\helpers\Json;
use yii\web\NotFoundHttpException;
use yii\web\Response;
use common\components\extenders\AjaxObject;
use common\components\helpers\Fun;
use zhtfrontend\components\bases\BaseController;

class MessagelogController extends BaseController
{

    public function actionIndex()
    {
        $param['searchData'] = \Yii::$app->request->get();
        $pageSize = isset($param['pageSize']) ? $param['pageSize'] : 12;
        $data = MessageLog::find()->where(['site_id' => Fun::getSiteId()])->orderBy('id DESC');
        if (!empty($param['searchData'])) {
            $search_data = array_filter($param['searchData']);
            if (!empty($search_data['receiver'])) {
                $data->andWhere(['like', 'receiver_arr', $search_data['receiver']]);
            }
            if (!empty($search_data['send_group'])) {
                $data->andWhere(['like', 'send_group', $search_data['send_group']]);
            }
        }
        $pages = new Pagination(['totalCount' => $data->count(), 'pageSize' => $pageSize]);
        isset($param['page']) ? $pages->page = $param['page'] - 1 : 0;
        $model = $data->offset($pages->offset)->limit($pages->limit)->all();

        $array = Fun::asArray($model, ['admin']);

        return $this->render('index', ['data' => $array, 'pages' => $pages]);
    }

    public function actionFailedsms()
    {
        $param['searchData'] = \Yii::$app->request->get();
        $pageSize = isset($param['pageSize']) ? $param['pageSize'] : 12;
        $data = MessageLog::find()->where(['site_id' => Fun::getSiteId(), 'send_group' => 'sms'])->andWhere('fail_arr_sms!=""')->orderBy('id DESC');
        if (!empty($param['searchData'])) {
            $search_data = array_filter($param['searchData']);
            if (!empty($search_data['tel'])) {
                $data->andWhere(['like', 'receiver_arr', $search_data['tel']]);
            }
        }
        $pages = new Pagination(['totalCount' => $data->count(), 'pageSize' => $pageSize]);
        isset($param['page']) ? $pages->page = $param['page'] - 1 : 0;
        $model = $data->offset($pages->offset)->limit($pages->limit)->all();
        $array = Fun::asArray($model, ['admin']);
        return $this->render('failedsms', ['data' => $array, 'pages' => $pages]);
    }

    public function actionCreate()
    {
        $this->layout = false;
        $model = new MessageLog();
        $form = new BuildForm('@app/components/forms/messagelogForm.php');
        $form->loadModel(['messagelog' => $model]);
        if ($form->loadData(\Yii::$app->request->post()) && $form->validate()) {
            $model = $form->getModel('messagelog');
            $model->send_way = serialize($model->send_way);
            $model->site_id = Fun::getSiteId();
            $model->admin_id = 1;
            if ($model->save(false)) {
                return $this->redirectMessage('添加成功', Fun::url(['Messagelog/index']), true);
            }
        }
        return $this->render('create', ['form' => $form->render()]);
    }

    public function actionDetails()
    {
        $details = MessageLog::findOne(\Yii::$app->request->get('id'));
        if ($details == null) {
            throw new NotFoundHttpException(\Yii::t('zhtfrontend', '信息不存在'));
        }
        return $this->render('details', ['details' => $details]);

    }


}
